// EquipDataSourceManager.h: interface for the DataSourceManager class.
//
//////////////////////////////////////////////////////////////////////

#if !defined _EQUIPDATASOURCE_H
#define  _EQUIPDATASOURCE_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxtempl.h>

class EquipDataSourceItem
{
public:
	EquipDataSourceItem() {}
	~EquipDataSourceItem() {}
	
public:
	const CString& Uri() const 
	{ return m_ScriptURI; 	}
	void Uri( const CString& uri )
	{ m_ScriptURI = uri; }
	
	const CString Path() const 
	{
		int iPos = Uri().ReverseFind ( _T ( '\\' ) );
		if ( iPos == -1 )
			return "";
		return Uri().Left( iPos + 1 ); 
	}
	const CString FileName() const
	{
		int iPos = Uri().ReverseFind ( _T ( '\\' ) );
		if ( iPos == -1 )
			return "";
		return Uri().Mid ( iPos + 1 );
	}
	
	void FormatName( const CString& fn ) { m_FormatName = fn; }	
	const CString& FormatName() const { return m_FormatName; }
	
	const CString& QueryStatement() const { return m_QueryStatement; }
	void QueryStatement( const CString& sql ) { m_QueryStatement = sql; }
	
	_ConnectionPtr* Connection() const { return m_DataSourceConnection; }
	void Connection( _ConnectionPtr* conn ) { m_DataSourceConnection = conn; }
			
private:
	CString m_QueryStatement;
	CString m_ScriptURI;
	CString m_FormatName;
	_ConnectionPtr* m_DataSourceConnection;
};

class EquipDataSourceManager
{
public:
	EquipDataSourceManager() 
	{ 
		m_CurDsi = 0;
	}
	~EquipDataSourceManager()
	{
		if ( m_CurDsi ) delete m_CurDsi; 
		for(int i = 0; i < m_Items.GetSize(); i++)
			delete m_Items.GetAt(i);
		m_Items.RemoveAll();
	}
	
public:
	const EquipDataSourceItem* CurrentDataSourceItem() const 
	{
		return m_CurDsi;	
	}
	const EquipDataSourceItem* GetDSItemByPFID( const CString& pfid ) const
	{
		for ( int i = 0; i < GetCount(); i++ )
		{
			if ( m_Items.GetAt(i)->m_CurDsi->FormatName() == pfid )
				return m_Items.GetAt(i)->m_CurDsi;
		}
			return 0;
	}
	const EquipDataSourceManager* GetDSMByPFID( const CString& pfid ) const
	{
		for( int i = 0; i < GetCount(); i++)
		{
			if(m_Items.GetAt(i)->m_CurDsi->FormatName() == pfid)
				return m_Items.GetAt(i);
		}
		return 0;
	}
	
public:
	void CurrentDataSourceItem( EquipDataSourceItem* dsi )
	{
		m_CurDsi = dsi ;
	}
	void AddAttachDSM(EquipDataSourceManager* pDSM)
	{
		ASSERT(pDSM);
		m_Items.Add(pDSM);
	}
	
private:
	const EquipDataSourceItem* GetItemAt( int index ) const 
	{
		return m_Items.GetAt( index )->CurrentDataSourceItem(); 
	}
	int GetCount() const { return m_Items.GetSize(); }
	
private:
	EquipDataSourceItem* m_CurDsi;
	CArray<EquipDataSourceManager*,EquipDataSourceManager*> m_Items;
};
typedef EquipDataSourceManager DataSourceManager;
typedef EquipDataSourceItem    DataSourceItem   ;

extern "C" __declspec(dllexport) AcDbEntity*  CreateEntity( DataSourceManager* pdsm );

#endif // !defined(AFX_DATASOURCEMANAGER_H__0EE8FB56_78AB_4573_90B5_27E4605BE132__INCLUDED_)
